package com.house.controller.login;


import com.house.entity.SystemUser;
import com.house.result.Result;
import com.house.service.LoginService;
import com.house.service.SystemUserService;
import com.house.utils.LoginUser;
import com.house.utils.LoginUserHolder;
import com.house.vo.login.CaptchaVo;
import com.house.vo.login.LoginVo;
import com.house.vo.system.user.SystemUserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "后台管理系统登录管理")
@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private SystemUserService service;

    @Operation(summary = "获取图形验证码")
    @GetMapping("login/captcha")
    public Result<CaptchaVo> getCaptcha() {
        CaptchaVo result= loginService.getCaptcha();
        return Result.ok(result);
    }

    @Operation(summary = "登录")
    @PostMapping("login")
    public Result<String> login(@RequestBody LoginVo loginVo) {
        String resilt= loginService.login(loginVo);
        return Result.ok(resilt);
    }

    @Operation(summary = "获取登陆用户个人信息")
    @GetMapping("info")
    public Result<SystemUserInfoVo> info() {
       Long userId = LoginUserHolder.getLoginUser().getUserId();
        SystemUser userinfo = service.getById(userId);
        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setName(userinfo.getName());
        systemUserInfoVo.setAvatarUrl(userinfo.getAvatarUrl());
        return Result.ok(systemUserInfoVo);
    }
}